package com.ytzl.itrip.controller.userinfo;

import com.ytzl.itrip.enuminfo.ItripUserErrorCodeEnum;
import com.ytzl.itrip.pojo.po.userinfo.ItripUser;
import com.ytzl.itrip.pojo.vo.userinfo.TokenVO;
import com.ytzl.itrip.service.userinfo.ItripUserService;
import com.ytzl.itrip.service.userinfo.TokenService;
import com.ytzl.itrip.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.regex.Pattern;

/**
 * Description: 本类用于
 * Created by gengshu on 2019/12/26 16:23
 */
@RestController
@Slf4j
@RequestMapping("/auth/api")
@Api(description = "登录认证")
public class LoginController {
    @Autowired
    private ItripUserService itripUserService;
    @Autowired
    private TokenService tokenService;

    @PostMapping("/dologin")
    @ApiModelProperty("验证邮箱密码，进而登录")
    public ResultUtil doLogin(@RequestParam("name")String email,
                              @RequestParam("password")String pwd,
                              HttpServletRequest request){
        //TODO  认证
        if (StringUtils.isEmpty(email)||!validEmail(email)||StringUtils.isEmpty(pwd)){
            return ResultUtil.failed(ItripUserErrorCodeEnum.INVALID_PARAM.getStatusMessage(),
                    ItripUserErrorCodeEnum.INVALID_PARAM.getStatusCode());
        }
        String userAgent = request.getHeader("user-agent");
        ItripUser itripUser = null;
        try {
            itripUser = itripUserService.isLoginSuccess(email,pwd);
            if (itripUser.getActivated()==0){
                return ResultUtil.failed(ItripUserErrorCodeEnum.NO_ACTIVATED.getStatusMessage(),
                        ItripUserErrorCodeEnum.NO_ACTIVATED.getStatusCode());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.failed(ItripUserErrorCodeEnum.LOGIN_ERROR.getStatusMessage(),
                    ItripUserErrorCodeEnum.LOGIN_ERROR.getStatusCode());
        }
        if (itripUser == null){
            return ResultUtil.failed(ItripUserErrorCodeEnum.LOGIN_SUCCESS.getStatusMessage(),
                    ItripUserErrorCodeEnum.LOGIN_SUCCESS.getStatusCode());
        }else{
            //生成token
            String token = tokenService.generatorToken(userAgent,itripUser);
            //保存token到redis
            tokenService.saveToken(token,itripUser);
            TokenVO tokenVO = new TokenVO(token,
                    Calendar.getInstance().getTimeInMillis()+2*60*60*1000,
                    Calendar.getInstance().getTimeInMillis());
            return ResultUtil.success(tokenVO);
        }
    }


    /**			 *
     * 合法E-mail地址：
     * 1. 必须包含一个并且只有一个符号“@”
     * 2. 第一个字符不得是“@”或者“.”
     * 3. 不允许出现“@.”或者.@
     * 4. 结尾不得是字符“@”或者“.”
     * 5. 允许“@”前的字符中出现“＋”
     * 6. 不允许“＋”在最前面，或者“＋@”
     */
    private static boolean validEmail(String email){
        String regex="^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$"  ;
        return Pattern.compile(regex).matcher(email).find();
    }
}
